package com.qq.input_monthy_unit.controller;


import com.alibaba.fastjson.JSONObject;
import com.qq.input_monthy_unit.entity.InputMonthyUnit;
import com.qq.input_monthy_unit.entity.LineInputTable;
import com.qq.input_monthy_unit.mapper.InputMonthyUnitMapper;
import com.qq.input_monthy_unit.service.impl.InputMonthyUnitServiceImpl;
import com.qq.input_monthy_unit.service.impl.LineInputTableServiceImpl;
import com.qq.utils.JsonChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杨清林
 * @since 2021-01-18
 */
@Controller
@RequestMapping("/input_monthy_unit")
public class InputMonthyUnitController {


    @Autowired
    InputMonthyUnitServiceImpl inputMonthyUnitService;

    @Autowired
    LineInputTableServiceImpl lineInputTableService;

    @RequestMapping("/test")
    public String test(Model model) {
        List<String> name_list = inputMonthyUnitService.queryByGroupName();
//       name_list.forEach(System.out::println);


        Map<String, List<Double>> name_monery_map = new HashMap<>();
        Map<String, List<String>> monthy_map = new HashMap<>();

        List<Map<String,Object>> maplist=new ArrayList<>();
        for (String name : name_list
        ) {
            List<InputMonthyUnit> inputMonthyUnitList = inputMonthyUnitService.queryAllMoneryByName(name);
            List<Double> moneryCollect = inputMonthyUnitList.stream().map(e -> e.getMonery()).collect(Collectors.toList());
//            List<String> monthyCollect = inputMonthyUnitList.stream().map(e -> e.getMonthy()).collect(Collectors.toList());
            Map<String,Object> mapdate=new HashMap<>();
            name_monery_map.put(name, moneryCollect);
            mapdate.put("name",name);
            mapdate.put("data",moneryCollect);

            maplist.add(mapdate);





            moneryCollect=null;
            inputMonthyUnitList=null;
//            monthyCollect=null;
        }



        List<String> monthy_list = inputMonthyUnitService.queryByGroupMonthy();
       // monthy_list.forEach(System.out::println);


        model.addAttribute("he","haha");



        String jsonStr =  JSONObject.toJSONString(maplist);
        model.addAttribute("d1",jsonStr);
        model.addAttribute("monthylist",JSONObject.toJSONString(monthy_list));


//-----------------------------图2-------------------------------------

        //查询所有的线条

        List<String> linelist = lineInputTableService.queryAllLine();
        //查询该月份
        List<String> monthyist = inputMonthyUnitService.queryByGroupMonthy();
        monthyist=monthyist.stream().sorted().collect(Collectors.toList());

        Map<String, Object> line_monery_map = new HashMap<>();


        List<Map<String, Object>> maplist2 = new ArrayList<>();
        List<LineInputTable> lineInputTablelist = lineInputTableService.queryAllLine_input();
        for (String line : linelist
        ) {

            //   List<Double> moneryCollect = inputMonthyUnitList.stream().map(e -> e.getMonery()).collect(Collectors.toList());
//            List<String> monthyCollect = inputMonthyUnitList.stream().map(e -> e.getMonthy()).collect(Collectors.toList());
//            List<LineInputTable> collect = lineInputTablelist.stream()
//                    .filter((e) -> e.getLine().equals(line)).collect(Collectors.toList());
            List<Double> monerylist = lineInputTableService.querySUMMoneryByLine(line);


            Map<String, Object> mapdate2 = new HashMap<>();
            mapdate2.put("name", line);
            mapdate2.put("data", monerylist);

            maplist2.add(mapdate2);


        }

        String jsonStr2 =  JSONObject.toJSONString(maplist2);

        Map<String, Object> haillist = maplist2.get(3);
        String haillistStr=JSONObject.toJSONString(haillist);
        model.addAttribute("haillist",haillistStr);
        maplist2.remove(3);

        String linelistStr=JSONObject.toJSONString(linelist);
        String jsonStr3 =  JSONObject.toJSONString(maplist2);

        model.addAttribute("d2",jsonStr2);
        model.addAttribute("linelistStr",linelistStr);
        model.addAttribute("d3",jsonStr3);



        return "pages/index";
    }

}
